<template>
  <div>
    <el-dialog
      :title="title + '流程'"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      width="600px"
      @closed="resetForm('form')"
    >
      <div>
        <el-form ref="form" :rules="rules" :model="form" label-width="80px">
          <el-form-item label="名称" prop="name">
            <el-input v-model.trim="form.name"></el-input>
          </el-form-item>
          <el-form-item label="业务范围">
            <el-select v-model="form.extFiled" placeholder="请选择">
              <el-option
                v-for="v in extFiledData"
                :key="v.value"
                :label="v.msg"
                :value="v.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="排序">
            <el-input-number
              v-model="form.orderBy"
              :min="1"
              :max="1000"
              size="medium"
              label="排序"
            ></el-input-number>
          </el-form-item>
          <el-form-item label="描述">
            <el-input
              type="textarea"
              :rows="2"
              placeholder="请输入描述"
              v-model="form.des"
            >
            </el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('form')">确 认</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  flowDefinitionAdd,
  flowDefinitionUpdate,
} from '@/api/admin/audit/flowDefinition.js'
import Bus from '@/utils/bus.js'
export default {
  data() {
    return {
      title: '新增',
      dialogVisible: false,
      addOrEdit: 1,
      checkedData: {},
      extFiledData: [],
      form: {
        name: '',
        scope: '1',
        extFiled: '',
        orderBy: 10,
        status: '0',
        des: '',
      },
      rules: {
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        extFiled: [
          { required: true, message: '请选择业务范围', trigger: 'blur' },
        ],
      },
    }
  },
  mounted() {
    Bus.$on('showDialogFlowAdd', (v) => {
      this.queryFlowType()
      if (v) {
        this.checkedData = v
        this.handleAddOrEdit(2)
      } else {
        this.handleAddOrEdit(1)
      }
    })
  },
  beforeDestroy() {
    Bus.$off('showDialogFlowAdd')
  },
  methods: {
    //显示弹窗
    handleAddOrEdit(type) {
      if (type == 2) {
        this.title = '编辑'
        for (let i in this.checkedData) {
          if (this.form.hasOwnProperty(i)) {
            this.form[i] = this.checkedData[i] || ''
          }
        }
        this.form.id = this.checkedData.id
      } else {
        this.title = '新增'
        delete this.form.id
      }
      this.addOrEdit = type
      this.dialogVisible = true
    },
    submitForm(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          this.addOrEdit == 1 ? this.add() : this.edit()
        }
      })
    },
    //新增
    add() {
      flowDefinitionAdd(this.form).then((res) => {
        if (!res || res.data.code != 0) return
        this.dialogVisible = false
        Bus.$emit('refreshFlowList')
        this.$message({
          message: '操作成功',
          type: 'success',
        })
      })
    },
    //编辑
    edit() {
      flowDefinitionUpdate(this.form).then((res) => {
        if (!res || res.data.code != 0) return
        this.dialogVisible = false
        Bus.$emit('refreshFlowList')
        this.$message({
          message: '操作成功',
          type: 'success',
        })
      })
    },
    //查询流程类型
    async queryFlowType() {
      this.extFiledData = await this.$store.dispatch(
        'getDictionaryEnumListVal',
        'FLOW_TYPE',
      )
    },
    //重置表单
    resetForm(form) {
      this.$refs[form].resetFields()
      this.form.name = ''
      this.form.extFiled = ''
      this.form.status = '0'
      this.form.orderBy = 10
      this.form.des = ''
      this.form.scope = '1'
    },
  },
}
</script>